package com.lianboke.link.ad.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lianboke.link.ad.R;
import com.lianboke.link.ad.activity.OrderTypeDetailsForHuZhuActivity;
import com.lianboke.link.ad.activity.OrderTypeDetailsForShiWanActivity;
import com.lianboke.link.ad.activity.OrderTypeDetailsForZhuCeActivity;
import com.lianboke.link.ad.activity.OrderTypeDetailsForZhuangJiActivity;
import com.lianboke.link.ad.beans.OrderListNewModel;
import com.lianboke.link.ad.utils.OrderStatusUtils;
import com.lianboke.link.ad.utils.imageloader.GlideLoader;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 * 类型任务数据适配器
 * Created by admin on 2018/1/5.
 */

public class OrderTypeAdapter extends BaseAdapter {

    private Context context;
    private HashMap<Integer,Boolean> itempSelect = new HashMap<Integer,Boolean>();

    private List<OrderListNewModel> datas = new ArrayList<OrderListNewModel>();


    public OrderTypeAdapter(Context con){
        context = con;
    }

    /**
     * s
     * @param data
     */
    public void setOrderDatas(List<OrderListNewModel> data){
        if(datas!=null){
            datas.clear();
        }
        datas.addAll(data);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if(datas==null){
            return 0;
        }
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(convertView==null){
            viewHolder = new ViewHolder();
            convertView =  LayoutInflater.from(context).inflate(R.layout.item_select_lable_layout,null);
            viewHolder.setmIVOrderIcon((ImageView)convertView.findViewById(R.id.iv_order_icon));
            viewHolder.setmTextOrderTitle((TextView) convertView.findViewById(R.id.tv_select_lable_title));
            viewHolder.setmTextOrderDes((TextView) convertView.findViewById(R.id.tv_select_lable_des));
            viewHolder.setmIVOrderMark((ImageView) convertView.findViewById(R.id.iv_order_mark));
            viewHolder.setLl_select_lable_content((LinearLayout) convertView.findViewById(R.id.ll_select_lable_content));
            viewHolder.setmTextOrderStatus((TextView) convertView.findViewById(R.id.tv_order_status));
            convertView.setTag(viewHolder);
        }else{
            viewHolder =(ViewHolder) convertView.getTag();
        }
        if(itempSelect.get(position)==null){
            itempSelect.put(position,false);
        }
        OrderListNewModel orderListNewModel = (OrderListNewModel)getItem(position);
        if(TextUtils.isEmpty(orderListNewModel.getImg())){
            viewHolder.getmIVOrderIcon().setBackgroundResource(R.drawable.icon_default);
        }else{
            GlideLoader.loadImage(orderListNewModel.getImg(),viewHolder.getmIVOrderIcon());
        }

        viewHolder.getmTextOrderTitle().setText(orderListNewModel.getName());
        viewHolder.getmTextOrderDes().setText(orderListNewModel.getDesc());
        String strOrderStatus = "";
        if(orderListNewModel.getType()==2){
            //注册
            strOrderStatus = OrderStatusUtils.getOrderStatusByZuCe(orderListNewModel.getStatus());
        }else{
            strOrderStatus = OrderStatusUtils.getOrderStatusByZhuangJi(orderListNewModel.getStatus());
        }
        viewHolder.getmTextOrderStatus().setText(strOrderStatus);
        //任务详情
        viewHolder.getLl_select_lable_content().setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent mIntent = new Intent();
                mIntent.putExtra("order_id", String.valueOf(orderListNewModel.getId()));
                mIntent.putExtra("order_type", String.valueOf(orderListNewModel.getType()));
                switch (orderListNewModel.getType()) {
                    case 1:
                        //装机任务
                        mIntent.setClass(context, OrderTypeDetailsForZhuangJiActivity.class);
                        context.startActivity(mIntent);
                        break;
                    case 2:
                        //注册任务
                        mIntent.setClass(context, OrderTypeDetailsForZhuCeActivity.class);
                        context.startActivity(mIntent);
                        break;
                    case 3:
                        //试玩任务
                        mIntent.setClass(context, OrderTypeDetailsForShiWanActivity.class);
                        context.startActivity(mIntent);
                        break;
                    case 4:
                        //互助任务
                        mIntent.setClass(context, OrderTypeDetailsForHuZhuActivity.class);
                        context.startActivity(mIntent);
                        break;
                }
            }
        });

        //view 信息展开
        viewHolder.getmIVOrderMark().setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                if(itempSelect.get(position)){
                    itempSelect.put(position,false);
                    viewHolder.getmIVOrderMark().setImageResource(R.drawable.icon_down_gb);
                }else{
                    itempSelect.put(position,true);
                    viewHolder.getmIVOrderMark().setImageResource(R.drawable.iocn_up_bg);
                }
                viewHolder.getmTextOrderDes().setVisibility(View.VISIBLE);
                notifyDataSetChanged();
            }
        });
        if(itempSelect.get(position)){
            viewHolder.getmIVOrderMark().setImageResource(R.drawable.iocn_up_bg);
            viewHolder.getmTextOrderDes().setVisibility(View.VISIBLE);
        }else{
            viewHolder.getmIVOrderMark().setImageResource(R.drawable.icon_down_gb);
            viewHolder.getmTextOrderDes().setVisibility(View.GONE);
        }
        return convertView;
    }


    /**
     *view 对象
     */
    public class ViewHolder{
        private ImageView mIVOrderIcon;
        private TextView mTextOrderTitle;
        private TextView mTextOrderDes;
        private ImageView mIVOrderMark;
        private LinearLayout ll_select_lable_content;
        private TextView mTextOrderStatus;

        public ViewHolder() {
        }

        public ImageView getmIVOrderIcon() {
            return mIVOrderIcon;
        }

        public void setmIVOrderIcon(ImageView mIVOrderIcon) {
            this.mIVOrderIcon = mIVOrderIcon;
        }

        public TextView getmTextOrderTitle() {
            return mTextOrderTitle;
        }

        public void setmTextOrderTitle(TextView mTextOrderTitle) {
            this.mTextOrderTitle = mTextOrderTitle;
        }

        public TextView getmTextOrderDes() {
            return mTextOrderDes;
        }

        public void setmTextOrderDes(TextView mTextOrderDes) {
            this.mTextOrderDes = mTextOrderDes;
        }

        public ImageView getmIVOrderMark() {
            return mIVOrderMark;
        }

        public void setmIVOrderMark(ImageView mIVOrderMark) {
            this.mIVOrderMark = mIVOrderMark;
        }

        public LinearLayout getLl_select_lable_content() {
            return ll_select_lable_content;
        }

        public void setLl_select_lable_content(LinearLayout ll_select_lable_content) {
            this.ll_select_lable_content = ll_select_lable_content;
        }

        public TextView getmTextOrderStatus() {
            return mTextOrderStatus;
        }

        public void setmTextOrderStatus(TextView mTextOrderStatus) {
            this.mTextOrderStatus = mTextOrderStatus;
        }
    }
}
